package GUI.forms;

import i18n.LanguageUtils;

import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.security.auth.login.Configuration;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextField;

import GUI.GUI;
import GUI.common.ExceptionI18N;


public abstract class GeneralForm extends JPanel {
	protected GUI gui;
	protected DecimalFormat formatDecimal = new DecimalFormat(LanguageUtils.getMessage("patternNumber"));
	protected SimpleDateFormat formatData = new SimpleDateFormat(LanguageUtils.getMessage("patternData"));
	
	public GeneralForm(GUI gui) {
		this.gui=gui;
		formatDecimal.setDecimalFormatSymbols(new DecimalFormatSymbols(LanguageUtils.getCurrentLocale()));
	}	

	protected void mandatory(Date date, String title, String msg) throws ExceptionI18N {		
		System.out.println("date : "+date);
		if (date==null) throw new ExceptionI18N(title, msg);
	}

	
	protected void mandatory(JComboBox combo, String title, String msg) throws ExceptionI18N {
		if (this.getSelectedCombo(combo)==null) throw new ExceptionI18N(title, msg);
	}

	protected void mandatory(JTextField text, String title, String msg) throws ExceptionI18N {
		if (text.getText().equals("")) throw new ExceptionI18N(title, msg);
	}
	
	protected Object getSelectedCombo(JComboBox combo) {
		int index = combo.getSelectedIndex();
		return (index!=-1?combo.getItemAt(index):null);
	}

	public ActionListener getActionListener() {
		return this.gui;
	}
	
	protected abstract boolean validation() throws ExceptionI18N;

	public boolean valida() throws ExceptionI18N {
		return validation();
	}
	
	public Date getData(String s) throws ExceptionI18N{
		Date d=null;
	
		SimpleDateFormat sdf = new SimpleDateFormat(LanguageUtils.getMessage("patternData"));
		try {
			d = sdf.parse(s);
		} catch (ParseException e) {
			d=null;
		}
		return d;
	}
	
}
